package com.nxk.modules.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nxk.modules.core.dao.SysParamDao;
import com.nxk.modules.core.entity.BaseEntity;
import com.nxk.modules.core.entity.SysParam;
import com.nxk.modules.core.service.SysParamService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class SysParamServiceImpl extends ServiceImpl<SysParamDao, SysParam> implements SysParamService {

     @Override
     @Transactional(readOnly = false)
    public boolean save(SysParam sysParam) {
        if (StringUtils.isNotBlank(sysParam.getId())) {
            return super.updateById(sysParam);
        } else {
            return super.save(sysParam);
        }
    }

    @Override
    public IPage<SysParam> page(SysParam sysParam) {
         Page<SysParam> page = new Page<SysParam>(sysParam.getPageNo(), sysParam.getPageSize());
        QueryWrapper<SysParam> queryWrapper=new QueryWrapper<SysParam>();
        if (StringUtils.isNotBlank(sysParam.getParamName())){
            queryWrapper.lambda().like(SysParam::getParamName,sysParam.getParamName());
        }
        if (StringUtils.isNotBlank(sysParam.getParamKey())){
            queryWrapper.lambda().like(SysParam::getParamKey,sysParam.getParamKey());
        }
        if (StringUtils.isNotBlank(sysParam.getParamValue())){
            queryWrapper.lambda().like(SysParam::getParamValue,sysParam.getParamValue());
        }
        if (StringUtils.isNotBlank(sysParam.getIsSystem())){
            queryWrapper.lambda().eq(SysParam::getIsSystem,sysParam.getIsSystem());
        }
        queryWrapper.orderByAsc("sort").in("status",1,2);
        IPage<SysParam> result = super.page(page, queryWrapper);
        return result;
    }

    @Override
    @Transactional(readOnly = false)
    public boolean deleteById(String id) {
        return super.removeById(id);
    }

    /**
     * 根据键名查找键值
     * @param key
     * @return
     */
    @Override
    public SysParam queryByKey(String key) {
         QueryWrapper<SysParam> queryWrapper =new QueryWrapper<SysParam>();
         queryWrapper.eq("param_key",key);
         queryWrapper.eq("status", BaseEntity.STATUS_NORMAL);
        return super.getOne(queryWrapper);
    }
}
